package org.hackystat.sdsa.analyzer.model.activity;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.logging.Logger;

import javax.xml.datatype.XMLGregorianCalendar;

import jess.Fact;

import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;
import org.hackystat.utilities.tstamp.Tstamp;
import org.junit.Test;

/**
 * Tests compilation action that causes compilation error or an error is 
 * diminished.  
 * 
 * @author Hongbing Kou
 */
public class TestCompilationActivity extends ActivityTestBase {
  /** Sensor data. */
  private SensorData sensorData;
  private Logger logger; 
  
  /**
   * Prepares test variables.
   * 
   * @throws Exception If error occurs.
   */
  @Override public void setUp() throws Exception {
    super.setUp();
    
    this.sensorData = new SensorData();
    XMLGregorianCalendar tstamp = Tstamp.makeTimestamp("2005-12-01T08:30:00.000-10:00");
    sensorData.setTimestamp(tstamp);
    sensorData.setResource("file:/C:/cvs/work/com/foo/Bar.java");
    sensorData.setSensorDataType("DevEvent");
    
    this.logger = Logger.getLogger(TestCompilationActivity.class.getName());
  }
  
  /**
   * Tests compilation action.
   * 
   * @throws Exception If error happens.
   */
  @Test public void testCompilationAction() throws Exception {
    CompilationActivity action = new CompilationActivity(this.sensorData, this.logger);
    
    String errMsg = "Class name cannot be resolved";
    action.setErrorMessage(errMsg);
    assertEquals("Test error message", errMsg, action.getErrorMessage());
    assertEquals("Test compilation action string", 
        "2005-12-01T08:30:00.000-10:00 Bar.java COMPILE {" + 
        errMsg + "}", action.toString());    
  }

  /**
   * Tests conversion from action object to JESS fact.
   * 
   * @throws Exception If error while converting to Jess fact.
   */
  @Override public void testJessFactConversion() throws Exception {
    CompilationActivity action = new CompilationActivity(this.sensorData, this.logger);
    action.setErrorMessage("Exception is not caught.");
    
    Fact f = action.assertActivity(1, engine);
    assertTrue("Test action is asserted to working memory", f.getFactId() >= 0);    
  }
}
